/*
 * jQuery File Upload Plugin JS Example 8.8.2
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

/*jslint nomen: true, regexp: true */
/*global $, window, blueimp */

$(function() {
    'use strict';

    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url: 'ajax/uploadImage/',
        submit: function(e, data) {
            var array_color_name = new Array();
            var count = 0;
            var result = true;
            $('.color_name').each(function(index, value) {
                if ($(this).val() != '') {
                    if (array_color_name.indexOf($(this).val()) >= 0) {
                         alert('Màu "' + $(this).val() + '" bị trùng, vui lòng chọn màu khác!');
                        $(this).css("border","1px solid #FF0000");
                        result = false;
                        return false;
                    } else {
                       array_color_name[count] = $(this).val();
                        count++;
                    }
                } else {
                    alert('Vui lòng điền tên màu vào ô còn trống!');
                    $(this).css("border","1px solid #FF0000");
                    result = false;
                    return false;
                }
            });

            return result;
        }
    });

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
            'option',
            'redirect',
            window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
            )
            );

    if (window.location.hostname === 'blueimp.github.io') {
        // Demo settings:
        $('#fileupload').fileupload('option', {
            url: '//jquery-file-upload.appspot.com/',
            // Enable image resizing, except for Android and Opera,
            // which actually support image resizing, but fail to
            // send Blob objects via XHR requests:
            disableImageResize: /Android(?!.*Chrome)|Opera/
                    .test(window.navigator.userAgent),
            maxFileSize: 5000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
        });
        // Upload server status check for browsers with CORS support:
        if ($.support.cors) {
            $.ajax({
                url: '//jquery-file-upload.appspot.com/',
                type: 'HEAD'
            }).fail(function() {
                $('<div class="alert alert-danger"/>')
                        .text('Upload server currently unavailable - ' +
                        new Date())
                        .appendTo('#fileupload');
            });
        }
    } else {

        // Load existing files:
//            $('#fileupload').addClass('fileupload-processing');
//            $.ajax({
//                // Uncomment the following to send cross-domain cookies:
//                //xhrFields: {withCredentials: true},
//                url: $('#fileupload').fileupload('option', 'url'),
//                data: { product_id: $("#product_id").val()  },
//                dataType: 'json',
//                context: $('#fileupload')[0]
//            }).always(function() {
//                $(this).removeClass('fileupload-processing');
//            }).done(function(result) {
//                $(this).fileupload('option', 'done')
//                        .call(this, null, {result: result});
//            });


    }

});
